package com.dicsystems.myrec;

import java.util.List;

import com.dicsystems.myrec.dto.Category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CategoryAdapter extends ArrayAdapter<Category> {

	private LayoutInflater inflater;

	protected CategoryAdapter(Context context, List<Category> categories) {
		super(context, R.layout.category_list_row, categories);
		this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private static class ViewHolder {
		TextView category;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.category_list_row, null);
			holder = new ViewHolder();
			holder.category = (TextView)convertView.findViewById(R.id.category);
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder)convertView.getTag();
		}
		Category category = getItem(position);
		holder.category.setTag(category.id);
		holder.category.setText(category.name);
		return convertView;
	}
}
